package com.baox.agridata.synchronous.schedule;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.entity.abutment.XDRBaseDataEntity;
import com.baox.agridata.synchronous.entity.abutment.XDRCertificateEntity;
import com.baox.agridata.synchronous.entity.abutment.XDRHarmlessEntity;
import com.baox.agridata.synchronous.entity.abutment.XDRLiveStockMarketEntity;
import com.baox.agridata.synchronous.result.HttpResultResponse;
import com.baox.agridata.synchronous.service.LocalPushDataToScAgricultureService;
import com.baox.agridata.synchronous.util.HttpUtils;
import com.baox.agridata.synchronous.util.ObjectToQueryParamsUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 动物卫生监督管理平台----四川农业农村厅
 */

@Slf4j
@Component
@Configuration
public class LocalPushDataToScAgriculture {


    private static final String APP_ID = "640bbe420131473e93456ffd385cd033";
    private static final String TOKEN_URL = "https://open-dmp.scnynctxxzx.com/auth/getToken";
    private static final String BASE_DATA_URL = "https://open-dmp.scnynctxxzx.com/sync/data";
    private static final Integer SUCCESS_FLAG = 0; //成功的标志


    @Autowired
    private LocalPushDataToScAgricultureService localPushDataToScAgricultureService;


    /**
     * 养殖场、屠宰场、贩运主体统计数据
     */
    @Scheduled(cron = "0 17 0 1 * ?")
    public void pushXDRBaseData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {

            List<XDRBaseDataEntity> xdrBaseDataEntities = localPushDataToScAgricultureService.selectBaseDataToScAgriculture();
            String baseDataParamStr = JSON.toJSONString(xdrBaseDataEntities); //基础数据接口的参数字符串
            Map<String, String> baseDataHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", baseDataParamStr);
            baseDataHeaders.put("authorization", "Bearer " + token);
            baseDataHeaders.put("servercode", "S1014");
            Map<String, Object> baseDataParam = new HashMap<>();
            baseDataParam.put("data", baseDataParamStr);
            HttpResultResponse baseDataResult = HttpUtils.post(BASE_DATA_URL, baseDataParam, baseDataHeaders);
            JSONObject baseJsonObject = JSON.parseObject(baseDataResult.getBody());
            Integer baseDataResultCode = baseJsonObject.getInteger("code");
            String baseDataResultMessage = baseJsonObject.getString("message");
            log.info("baseDataResultCode:{},baseDataResultMessage:{}", baseDataResultCode, baseDataResultMessage);
            if (baseDataResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("养殖场、屠宰场、贩运主体统计数据推送成功");
            } else {
                log.error("养殖场、屠宰场、贩运主体统计数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    /**
     * 无害化处理厂备案数据对接
     */
    @Scheduled(cron = "0 17 0 1 * ?")
    public void pushHarmlessData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {
            List<XDRHarmlessEntity> xdrHarmlessEntities = localPushDataToScAgricultureService.selectHarmlessData();
            String harmlessParamStr = JSON.toJSONString(xdrHarmlessEntities); //无害化接口的参数字符串
            Map<String, String> baseDataHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", harmlessParamStr);
            baseDataHeaders.put("authorization", "Bearer " + token);
            baseDataHeaders.put("servercode", "S1015");
            Map<String, Object> harmlessDataParam = new HashMap<>();
            harmlessDataParam.put("data", harmlessParamStr);
            HttpResultResponse baseDataResult = HttpUtils.post(BASE_DATA_URL, harmlessDataParam, baseDataHeaders);
            JSONObject baseJsonObject = JSON.parseObject(baseDataResult.getBody());
            Integer harmlessResultCode = baseJsonObject.getInteger("code");
            String harmlessResultMessage = baseJsonObject.getString("message");
            log.info("harmlessResultCode:{},harmlessResultMessage:{}", harmlessResultCode, harmlessResultMessage);
            if (harmlessResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("无害化处理厂备案明细数据推送成功");
            } else {
                log.error("无害化处理厂备案明细数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    /**
     * 交易市场数据对接   stockMarket
     */
    @Scheduled(cron = "0 17 0 1 * ?")
    public void pushLiveStockMarketData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {
            List<XDRLiveStockMarketEntity> stockMarketEntities = localPushDataToScAgricultureService.selectLiveStockMarketData();
            String stockMarketParamStr = JSON.toJSONString(stockMarketEntities); //交易市场接口的参数字符串
            Map<String, String> stockMarketHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", stockMarketParamStr);
            stockMarketHeaders.put("authorization", "Bearer " + token);
            stockMarketHeaders.put("servercode", "S1016");
            Map<String, Object> stockMarketDataParam = new HashMap<>();
            stockMarketDataParam.put("data", stockMarketParamStr);
            HttpResultResponse sockMarketResult = HttpUtils.post(BASE_DATA_URL, stockMarketDataParam, stockMarketHeaders);
            JSONObject sockMarketObject = JSON.parseObject(sockMarketResult.getBody());
            Integer sockMarketResultCode = sockMarketObject.getInteger("code");
            String sockMarketResultMessage = sockMarketObject.getString("message");
            log.info("sockMarketResultCode:{},sockMarketResultMessage:{}", sockMarketResultCode, sockMarketResultMessage);
            if (sockMarketResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("交易市场备案明细数据推送成功");
            } else {
                log.error("交易市场备案明细数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    /**
     * 检疫证数据对接   certificate
     */
    @Scheduled(cron = "0 0 15 ? * MON")
    public void pushCertificateData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {
            List<XDRCertificateEntity> certificateEntities = localPushDataToScAgricultureService.selectCertificateData();
            String certificateParamStr = JSON.toJSONString(certificateEntities); //检疫证接口的参数字符串
            Map<String, String> certificateHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", certificateParamStr);
            certificateHeaders.put("authorization", "Bearer " + token);
            certificateHeaders.put("servercode", "S1017");
            Map<String, Object> certificateParam = new HashMap<>();
            certificateParam.put("data", certificateParamStr);
            HttpResultResponse certificateResult = HttpUtils.post(BASE_DATA_URL, certificateParam, certificateHeaders);
            JSONObject certificateObject = JSON.parseObject(certificateResult.getBody());
            Integer certificateResultCode = certificateObject.getInteger("code");
            String certificateResultMessage = certificateObject.getString("message");
            log.info("certificateResultCode:{},certificateResultMessage:{}", certificateResultCode, certificateResultMessage);
            if (certificateResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("检疫证数据推送成功");
            } else {
                log.error("检疫证数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    /**
     * 检疫证出省数据对接   pushOutProvinceCertificateData
     */
    @Scheduled(cron = "0 0 15 ? * MON")
    public void pushOutProvinceCertificateData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {
            List<XDRCertificateEntity> certificateEntities = localPushDataToScAgricultureService.selectOutProvinceData();
            String certificateParamStr = JSON.toJSONString(certificateEntities); //检疫证接口的参数字符串
            Map<String, String> certificateHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", certificateParamStr);
            certificateHeaders.put("authorization", "Bearer " + token);
            certificateHeaders.put("servercode", "S1018");
            Map<String, Object> certificateParam = new HashMap<>();
            certificateParam.put("data", certificateParamStr);
            HttpResultResponse certificateResult = HttpUtils.post(BASE_DATA_URL, certificateParam, certificateHeaders);
            JSONObject certificateObject = JSON.parseObject(certificateResult.getBody());
            Integer certificateResultCode = certificateObject.getInteger("code");
            String certificateResultMessage = certificateObject.getString("message");
            log.info("outCertificateResultCode:{},outCertificateResultMessage:{}", certificateResultCode, certificateResultMessage);
            if (certificateResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("出省检疫证数据推送成功");
            } else {
                log.error("出省检疫证数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    /**
     * 检疫证入省数据对接   pushInProvinceCertificateData
     */
    @Scheduled(cron = "0 0 15 ? * MON")
    public void pushInProvinceCertificateData() {
        String token = getToken();
        if (!StringUtils.isEmpty(token)) {
            List<XDRCertificateEntity> certificateEntities = localPushDataToScAgricultureService.selectInProvinceData();
            String certificateParamStr = JSON.toJSONString(certificateEntities); //检疫证接口的参数字符串
            Map<String, String> certificateHeaders = ObjectToQueryParamsUtil.convertToQueryParams("data", certificateParamStr);
            certificateHeaders.put("authorization", "Bearer " + token);
            certificateHeaders.put("servercode", "S1018");
            Map<String, Object> certificateParam = new HashMap<>();
            certificateParam.put("data", certificateParamStr);
            HttpResultResponse certificateResult = HttpUtils.post(BASE_DATA_URL, certificateParam, certificateHeaders);
            JSONObject certificateObject = JSON.parseObject(certificateResult.getBody());
            Integer certificateResultCode = certificateObject.getInteger("code");
            String certificateResultMessage = certificateObject.getString("message");
            log.info("inCertificateResultCode:{},inCertificateResultMessage:{}", certificateResultCode, certificateResultMessage);
            if (certificateResultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
                log.info("入省检疫证数据推送成功");
            } else {
                log.error("入省检疫证数据推送失败！！！");
            }
        } else {
            log.info("token 获取失败");
        }
    }


    //1.先获取token，参数为appid
    private String getToken() {
        Map<String, String> tokenHeaders = ObjectToQueryParamsUtil.convertToQueryParams("appid", APP_ID);
        Map<String, String> tokenParam = new HashMap<>();
        tokenParam.put("appid", APP_ID);
        HttpResultResponse tokenResult = HttpUtils.post(TOKEN_URL, tokenParam, tokenHeaders);
        JSONObject jsonObject = JSON.parseObject(tokenResult.getBody());
        Integer resultCode = jsonObject.getInteger("code");
        if (resultCode.intValue() == SUCCESS_FLAG.intValue()) { //当获取到token后，在继续后续的操作
            String tokenValue = jsonObject.getJSONObject("data").getString("token");  //token
            return tokenValue;
        }
        return null;
    }


}
